package com.zabra.zl.ui.allocation_manage.presenter;

import com.zabra.zl.App;
import com.zabra.zl.base.BasePresenter;
import com.zabra.zl.data.AllocationManageBean;
import com.zabra.zl.ui.allocation_manage.contract.AllocationManageContract;
import com.zabra.zl.ui.allocation_manage.model.AllocationManageModel;
import com.zabra.zl.utils.SharedPreferencesUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AllocationManagePresenter extends BasePresenter<AllocationManageContract.View> implements AllocationManageContract.Presenter {
    private AllocationManageContract.Model model;

    public AllocationManagePresenter() {
        model = new AllocationManageModel();
    }

    @Override
    public void getData(String bpStatus, String bpType, String currentPage, String itemsPerPage) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getData(bpStatus, bpType, currentPage, itemsPerPage).enqueue(new Callback<AllocationManageBean>() {
            @Override
            public void onResponse(Call<AllocationManageBean> call, Response<AllocationManageBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token", response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<AllocationManageBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }

    @Override
    public void getDataFresh(String bpStatus, String bpType, String currentPage, String itemsPerPage) {
        if (!isViewAttached()) {
            return;
        }
        model.getData(bpStatus, bpType, currentPage, itemsPerPage).enqueue(new Callback<AllocationManageBean>() {
            @Override
            public void onResponse(Call<AllocationManageBean> call, Response<AllocationManageBean> response) {
                mView.onSuccessFresh(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token", response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<AllocationManageBean> call, Throwable t) {
                mView.onError(t);
            }
        });
    }

    @Override
    public void getDataLoadMore(String bpStatus, String bpType, String currentPage, String itemsPerPage) {
        if (!isViewAttached()) {
            return;
        }
        model.getData(bpStatus, bpType, currentPage, itemsPerPage).enqueue(new Callback<AllocationManageBean>() {
            @Override
            public void onResponse(Call<AllocationManageBean> call, Response<AllocationManageBean> response) {
                mView.onSuccessLoadMore(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token", response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<AllocationManageBean> call, Throwable t) {
                mView.onError(t);
            }
        });
    }
}
